<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>信贷管理系统</h2>
        </div>
      </template>
      
      <el-form 
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="0"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading"
            class="login-button" 
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>

        <!-- <div class="tips">
          <p>默认账号：admin</p>
          <p>默认密码：123456</p>
        </div> -->
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/user'

export default {
  name: 'Login',
  components: {
    User,
    Lock
  },
  setup() {
    const router = useRouter()
    const loginFormRef = ref(null)
    const loading = ref(false)

    const loginForm = reactive({
      username: 'admin',
      password: '123456'
    })

    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }

    const handleLogin = async () => {
      try {
        await loginFormRef.value.validate()
        loading.value = true
        
        try {
          // 调用真实的登录接口
          const res = await login({
            username: loginForm.username,
            password: loginForm.password
          })
          
          console.log('登录返回数据:', res)
          // 支持字符串类型的code和数字类型的code
          if (res.code === 200 || res.code === '200') {
            // 支持不同格式的数据结构
            const token = res.data.token || res.data.tokenValue || ''
            const tokenHead = res.data.tokenHead || res.data.tokenType || ''
            
            // 清除之前的数据
            localStorage.clear()
            
            // 存储新的数据
            localStorage.setItem('token', tokenHead ? `${tokenHead} ${token}` : token)
            
            // 存储用户信息(如果返回)
            if (res.data.userInfo) {
              localStorage.setItem('userInfo', JSON.stringify(res.data.userInfo))
            }
            
            localStorage.setItem('routesAdded', 'false') // 重置路由添加标记
            
            ElMessage.success(res.msg || '登录成功')
            router.push('/welcome')
          } else {
            ElMessage.error(res.msg || '登录失败')
          }
        } catch (error) {
          console.error('登录错误:', error)
          ElMessage.error(error.message || '登录失败')
        } finally {
          loading.value = false
        }
      } catch (error) {
        console.error('表单验证失败', error)
        loading.value = false
      }
    }

    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('/Hippopx.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-card {
  width: 400px;
}

.card-header {
  text-align: center;
}

.card-header h2 {
  margin: 0;
  font-size: 24px;
  color: #409EFF;
}

.login-form {
  padding: 20px 0;
}

.login-button {
  width: 100%;
}

.tips {
  margin-top: 20px;
  color: #666;
}

.tips p {
  margin: 5px 0;
  font-size: 14px;
}
</style>